//
//  BassEventViewController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/23.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BassEventViewController.h"



#import "MatchScoreEventModel.h"

#import "matchEventViewCell.h"

//球队
#import "teamRankViewCell.h"

#import "teamRankModel.h"



@interface BassEventViewController ()


//历史对战
@property(nonatomic,strong)NSMutableArray *historyVSModelArray;


//主队近期
@property(nonatomic,strong)NSMutableArray *hostRecentMatchModelArray;

//客队近期
@property(nonatomic,strong)NSMutableArray *visitRecentMatchModelArray;

//主队未来
@property(nonatomic,strong)NSMutableArray *hostFutureMatchModelArray;

//客队未来
@property(nonatomic,strong)NSMutableArray *visitFutureMatchModelArray;





@property(nonatomic,strong)UITableView *eventTableView;
//主队比分
@property(nonatomic,strong)NSMutableArray *hostScoresModelArray;

//客队比分
@property(nonatomic,strong)NSMutableArray *visitScoresModelArray;


//球队

//主队
@property(nonatomic,strong)NSMutableArray *hostRankModelArray;

//客队
@property(nonatomic,strong)NSMutableArray *visitRankModelArray;


@property(nonatomic,copy)NSString *hostIntro;
@property(nonatomic,copy)NSString *visitIntro;

//主队--客队名字
@property(nonatomic,copy)NSString *hostTeam;
@property(nonatomic,copy)NSString *visitTeam;




//主队--Vs
@property(nonatomic,copy)NSString *historyVsDescStr;

//主队--近期评价
@property(nonatomic,copy)NSString *hostRecentDescStr;
//客队--近期评价
@property(nonatomic,copy)NSString *visitRecentDescStr;




@end

@implementation BassEventViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.scrollEnabled = NO;
    
    
    // 添加空白的 footView , 防止界面显示不全
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(5))];
    self.tableView.tableFooterView = footerView;
    
    
    
    //主队比分
    self.hostScoresModelArray= [NSMutableArray array];
    
    //客队比分
    self.visitScoresModelArray= [NSMutableArray array];
    
    
    self.hostRankModelArray = [NSMutableArray array];
    self.visitRankModelArray = [NSMutableArray array];
    
    
    
    //事件
    [self JsonJieXiForDetailEvent];
    
    
    //和球队排名
    [self jsonJieXiForTeamRank];
    
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


#pragma mark  事件
-(void)JsonJieXiForDetailEvent
{
    
    NSString *urlString = @"http://api.caipiao.163.com/jclq_live_matchDetail.html?mobileType=android&ver=4.8&channel=wap&apiVer=1.1&apiLevel=16";
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.visitId,@"visitId",self.hostId,@"hostId",@"11",@"type",self.matchId,@"matchId", nil];
    
    
    
    [LORequestManger  POST:urlString params:params success:^(id response) {
        NSDictionary *bigDict =( NSDictionary*)response;
        
        NSDictionary *smallDict = [bigDict objectForKey:@"data"];
        
        
        NSArray *hostScoresArray = [smallDict objectForKey:@"hostScores"];
        NSArray *visitScoresArray = [smallDict objectForKey:@"visitScores"];
        
        
        
        MatchScoreEventModel *model =   [[MatchScoreEventModel alloc]init];
        model.teamName = [smallDict objectForKey:@"hostName"];
        model.firstScore = hostScoresArray[0];
        model.secondScore = hostScoresArray[1];
        model.thirdScore = hostScoresArray[2];
        model.fourthScore = hostScoresArray[3];
        model.totalScore = [hostScoresArray lastObject];
        [self.hostScoresModelArray addObject:model];
        
        
        MatchScoreEventModel *model2 =   [[MatchScoreEventModel alloc]init];
        model2.teamName = [smallDict objectForKey:@"visitName"];
        model2.firstScore = visitScoresArray[0];
        model2.secondScore = visitScoresArray[1];
        model2.thirdScore = visitScoresArray[2];
        model2.fourthScore = visitScoresArray[3];
        model2.totalScore = [visitScoresArray lastObject];
        
        [self.visitScoresModelArray addObject:model2];
        
        
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        NSLog(@"网络错误");
    }];
    
    
}

#pragma mark  球队排名--解析
-(void)jsonJieXiForTeamRank
{
    
    NSString *urlString = @"http://api.caipiao.163.com/jclq_live_matchDetail.html?mobileType=android&ver=4.8&channel=wap&apiVer=1.1&apiLevel=16";
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.visitId,@"visitId",self.hostId,@"hostId",@"13",@"type",self.matchId,@"matchId", nil];
    
    
    
    [LORequestManger  POST:urlString params:params success:^(id response)
     {
         NSDictionary *bigDict =( NSDictionary*)response;
         
         NSDictionary *smallDict = [bigDict objectForKey:@"data"];
         
         
         NSArray *rankingListArray = [smallDict objectForKey:@"rankingList"];
         
         
         
         
         NSArray *ranking = [rankingListArray[0] objectForKey:@"ranking"];
         
         self.hostIntro = [rankingListArray[0] objectForKey:@"rankingDesc"];
         for(NSDictionary *dict   in  ranking)
         {
             teamRankModel *model = [teamRankModel teamRankModelWithDict:dict];
             [self.hostRankModelArray addObject:model];
             
         }
         
         NSArray *visitRanking = [rankingListArray[1] objectForKey:@"ranking"];
         self.visitIntro = [rankingListArray[1] objectForKey:@"rankingDesc"];
         for(NSDictionary *dict   in  visitRanking)
         {
             teamRankModel *model = [teamRankModel teamRankModelWithDict:dict];
             [self.visitRankModelArray addObject:model];
             
         }
         
         
         [self.tableView reloadData];
         
         
         // 数据加载完毕, 刷新视图后, 发送一个通知, 将 contentSize 发送回去
         [[NSNotificationCenter defaultCenter] postNotificationName:@"basketballEvent" object:nil];
         
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
         NSLog(@"网络错误");
     }];
    
    
    
}






#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}


#pragma mark - 自定义分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    SectionHeaderView *headerView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(30))];
    headerView.headerLabel.textAlignment = 1;
    if(section ==0)
    {
        headerView.headerLabel.text =  @"比赛事件";
    }
    else
    {
        headerView.headerLabel.text =  @"球队事件";
    }
    
    return headerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kScreenIp6(30);
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section ==0)
    {
        return self.hostScoresModelArray.count+ self.visitScoresModelArray.count + 1  ;
        
    }
    else
    {
        return self.hostRankModelArray.count + 2 + self.visitRankModelArray.count + 2 ;
    }
    

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section ==0)
    {
        {
            static NSString *cellIndetifier = @"eventCell";
            [tableView registerClass:[matchEventViewCell class] forCellReuseIdentifier:cellIndetifier];
            
            matchEventViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            
            if(indexPath.row ==0)
            {
                
                [cell customTextColor];
                return cell;
                
            }
            else if(indexPath.row ==1)
            {
                MatchScoreEventModel *model = self.hostScoresModelArray[indexPath.row-1];
                
                [cell setCellValueWithModel:model];
                
                return cell;
                
            }
            else
            {
                MatchScoreEventModel*model = self.visitScoresModelArray[indexPath.row - 2];
                
                
                
                [cell setCellValueWithModel:model];
                
                return cell;
                
            }
            
            
        }
        
        
    }
    else
    {
    
        if(indexPath.row ==0) //主队
        {
            static NSString *cellIndetifier = @"defaultCell123";
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier ];
            if(cell ==nil)
            {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
            }
            
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.textLabel.text = self.hostIntro;
            
            //线
            UIView *footLineView = [[UIView  alloc]initWithFrame:CGRectMake( 0, 35, kScreenWidth, 0.5)];
            
            footLineView.backgroundColor = [UIColor colorWithWhite:0.699 alpha:1.000];
            
            [cell.contentView addSubview:footLineView];
            
            cell.selectionStyle = UITableViewCellSeparatorStyleNone;
            return cell;
            
        }
        else if(indexPath.row ==1)
        {
            static NSString *cellIndetifier = @"rankCell";
            [tableView registerClass:[teamRankViewCell class] forCellReuseIdentifier:cellIndetifier];
            
            teamRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            [cell customTextColor];
            
            return cell;
        }
        else if (indexPath.row ==2)
            
        {
            static NSString *cellIndetifier = @"rankCell";
            [tableView registerClass:[teamRankViewCell class] forCellReuseIdentifier:cellIndetifier];
            
            teamRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            
            if(self.hostRankModelArray.count != 0)
            {
                
                teamRankModel *model = self.hostRankModelArray[indexPath.row - 2];
                [cell  setCellValueWithModel:model];
                
            }
            
            return cell;
        }
        
        
        
        else if(indexPath.row == 3)//客队
        {
            static NSString *cellIndetifier = @"defaultCell";
            
            UITableViewCell *cellDea = [tableView dequeueReusableCellWithIdentifier:cellIndetifier ];
            if(cellDea ==nil)
            {
                cellDea = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
            }
            
            cellDea.textLabel.font = [UIFont systemFontOfSize:15];
            cellDea.textLabel.text = self.visitIntro;
            
            //线
            UIView *footLineView = [[UIView  alloc]initWithFrame:CGRectMake( 0, 35, kScreenWidth, 0.5)];
            
            footLineView.backgroundColor = [UIColor colorWithWhite:0.699 alpha:1.000];
            
            [cellDea.contentView addSubview:footLineView];
            
            cellDea.selectionStyle = UITableViewCellSeparatorStyleNone;

            return cellDea;
            
        }
        else if(indexPath.row ==4)
        {
            static NSString *cellIndetifier = @"rankCell";
            [tableView registerClass:[teamRankViewCell class] forCellReuseIdentifier:cellIndetifier];
            
            teamRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            
            [cell customTextColor];

            
            
            return cell;
            
            
        }
        else
        {
            static NSString *cellIndetifier = @"rankCell";
            [tableView registerClass:[teamRankViewCell class] forCellReuseIdentifier:cellIndetifier];
            
            teamRankViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
            
            
            
            if(self.visitRankModelArray.count != 0)
            {
                teamRankModel *model = self.visitRankModelArray[indexPath.row - 5];
                [cell  setCellValueWithModel:model];
            }
            
            return cell;
            
        }
}
 
}



@end
